package com.example.blog;

import com.example.blog.entity.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootApplication
public class BlogApplication {

    public static void main(String[] args) {

        SpringApplication.run(BlogApplication.class, args);

        System.out.println("成功");
    }

    @Bean
    public CommandLineRunner testDatabaseConnection(DataSource dataSource) {
        return args -> {
            try (Connection connection = dataSource.getConnection()) {
                // 测试数据库连接
                if (connection != null && !connection.isClosed()) {
                    System.out.println("成功连接到数据库");
                } else {
                    System.out.println("数据库连接失败");
                }
            } catch (SQLException e) {
                System.out.println("数据库连接测试失败");
                e.printStackTrace();
            }
        };
    }
}